package cn.e2win.biz.ds;

import java.sql.SQLException;
import java.util.Map;

import cn.e2win.base.entity.SysUser;
import cn.e2win.biz.security.UserSession;
import cn.e2win.biz.util.StringUtils;
import org.springframework.stereotype.Repository;

import cn.e2win.base.entity.BaseEntity;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAOHibernateImpl;
import cn.e2win.biz.base.CommonStatic;
import cn.e2win.biz.dbo.DsEmp;


@Repository
public class DsDaoImpl extends BaseDAOHibernateImpl implements DsDao {
	public static final String entitySql_DsEmp = "SELECT * FROM ("
			
			+ " SELECT t0.*"
			+ " , t1.label AS labelFocusOn "
			+ " , t2.label AS labelHelpFor "
			+ " , t3.label AS labelIndustry "
			+ " FROM ds_emp t0 "
			+ " LEFT JOIN ("+CommonStatic.labSql+") AS t1 ON t0.id = t1.entityId AND t1.entityType='DsEmp' AND t1.labelType='labelFocusOn'"
			+ " LEFT JOIN ("+CommonStatic.labSql+") AS t2 ON t0.id = t2.entityId AND t2.entityType='DsEmp' AND t2.labelType='labelHelpFor'"
			+ " LEFT JOIN ("+CommonStatic.labSql+") AS t3 ON t0.id = t3.entityId AND t3.entityType='DsEmp' AND t3.labelType='labelIndustry' "
			
			+ ") t "
			+ " WHERE 1=1 ";
	
	
	
	@Override
	public void pagerDs(Pager pager) {
		try {
			String sql = entitySql_DsEmp;
			String user_I = pager.getScMap().get("user_I");
			if(!StringUtils.isEmpty(user_I)){
				SysUser user = pager.getCurrUser();
				if(user_I.equals("admin")){
					sql += " And adminUserId="+user.getId()+" ";
				}
			}
			//用于搜索
			String title_I = pager.getScMap().get("title_I");
			if(!StringUtils.isEmpty(title_I)){
				sql += " And (empName like '%"+title_I+"%' or dwName like '%"+title_I+"%' ) ";
			}
			pager.processSql(sql + Pager.DEF_ORDER_BY);
			processPager(pager);
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	@Override
	public Map<String, Object>getEntity(BaseEntity entity){
		if(entity instanceof DsEmp){
			return super.get1stRecordBySql(entitySql_DsEmp + " AND id=" + entity.getId(), null);
		}else{
			return super.getEntity(entity);
		}
	}
}
